﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.Blog.DTO.BlogLogin;
using AnQinFrameWorkAPI.Entity.Blog.Entity;
using AutoMapper;
using FrameWork.JWT;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.Blog;

/// <summary>
///     博客登录业务层
/// </summary>
public class BlogLoginService
{
    //数据库
    private readonly DatabaseContext _context;

    // 自动映射器
    private readonly IMapper _mapper;

    //token生成器
    private readonly TokenService _tokenService;

    // 构造函数注入
    public BlogLoginService(DatabaseContext context, IMapper mapper, TokenService tokenService)
    {
        _context = context;
        _mapper = mapper;
        _tokenService = tokenService;
    }

    #region 获取邮箱验证码

    public async Task<RequestResponseModel> BlogEmailCode(BlogEmailModel blogEmailModel)
    {
        return RequestResponse.OK("OK");
    }

    #endregion

    #region 注册

    public async Task<RequestResponseModel> BlogRegister(BlogRegisterDTO blogRegisterDTO)
    {
        //查找账号
        var query = await _context.BlogUser.FirstOrDefaultAsync(a => a.Account == blogRegisterDTO.Account);

        //找到重复账号
        if (query != null) return RequestResponse.BadRequest("该邮箱已注册", blogRegisterDTO.Account);

        //生成新用户数据
        var blogUser = _mapper.Map<BlogUserModel>(blogRegisterDTO);
        blogUser.Id = Guid.NewGuid();
        blogUser.Code = "游客";
        blogUser.Name = "游客";
        blogUser.Email = blogUser.Account;
        blogUser.CreateTime = DateTime.Now;

        //生成token
        var token = _tokenService.GenerateToken(blogUser.Id.ToString());

        //存入登录表中
        var blogLogin = new BlogLoginModel
        {
            Id = Guid.NewGuid(),
            UserId = blogUser.Id,
            Token = token,
            CreateTime = DateTime.Now
        };

        //新用户存入库中
        _context.BlogLogin.Add(blogLogin);
        _context.BlogUser.Add(blogUser);
        await _context.SaveChangesAsync();

        //返回Token
        return RequestResponse.OK(token, "注册成功");
    }

    #endregion

    #region 登录

    public async Task<RequestResponseModel> BlogLogin(BlogLoginDTO blogLoginDTO)
    {
        //查找账号
        var query = await _context.BlogUser.FirstOrDefaultAsync(a => a.Account == blogLoginDTO.Account);

        //找不到账号
        if (query == null) return RequestResponse.BadRequest("该邮箱未注册，请先注册后登录！", blogLoginDTO.Account);

        //密码错误
        if (blogLoginDTO.Password != query.Password)
            return RequestResponse.BadRequest("账号或者密码错误！", blogLoginDTO.Password);

        //生成token
        var token = _tokenService.GenerateToken(query.Id.ToString());

        //存入登录表中
        var blogLogin = new BlogLoginModel
        {
            Id = Guid.NewGuid(),
            UserId = query.Id,
            Token = token,
            CreateTime = DateTime.Now
        };

        //新用户存入库中
        _context.BlogLogin.Add(blogLogin);
        await _context.SaveChangesAsync();

        //返回Token
        return RequestResponse.OK(token, "登录成功");
    }

    #endregion
}